package edu.wtbu.cs.androiddemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import edu.wtbu.cs.androiddemo.MainActivity;
import edu.wtbu.cs.androiddemo.R;
import edu.wtbu.cs.androiddemo.event.OuterClassClickListener;


/**
 *
 * @author
 * @description 事件监听Demo
 */
public class EventListenSampleActivity extends AppCompatActivity implements View.OnClickListener{

    public static String logTag = EventListenSampleActivity.class.getName();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_event_listen_sample);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });


        /** 匿名内部类作为事件监听器 */
        Button anonymousInnerClassButton = findViewById(R.id.anonymous_inner_class_button);
        anonymousInnerClassButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(EventListenSampleActivity.this,"匿名内部类作为事件监听器",Toast.LENGTH_LONG).show();
            }
        });

        /** 内部类作为事件监听器 */
        MyButtonListener myButtonListener = new MyButtonListener();
        Button innerClassButton = findViewById(R.id.inner_class_button);
        innerClassButton.setOnClickListener(myButtonListener);

        /** Activity作为事件监听器 */
        Button activityAsListenerButton = findViewById(R.id.activity_as_listener_button);
        activityAsListenerButton.setOnClickListener(this);

        /** 外部类监听器 */
        Button outerClassAsListenerButton = findViewById(R.id.outer_class_as_listener_button);
        outerClassAsListenerButton.setOnClickListener(new OuterClassClickListener("外部类作为监听器",this));


    }

    /**
     * 绑定到标签
     * @param view
     */
    public void onClickBindTagListenerButton(View view){
        Button bindTagListenerButton = findViewById(R.id.bind_to_tag_button);
        bindTagListenerButton.setText("按钮被单机了");
    }

    /**
     * 跳转到消息传递机制示例Demo
     * @param view
     */
    public void onClickUIMessageButton(View view){
        Log.i(logTag,"clickEventListenSampleButton");
        Intent intent = new Intent(EventListenSampleActivity.this, UIMessageSampleActivity.class);
        startActivity(intent);
    }
    @Override
    public void onClick(View v) {
        Toast.makeText(EventListenSampleActivity.this,"Activity类自身作为事件监听器",Toast.LENGTH_LONG).show();
    }

    class MyButtonListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            Toast.makeText(EventListenSampleActivity.this,"内部类作为事件监听器",Toast.LENGTH_LONG).show();
        }
    }
}


